# coding=utf-8
import sys
from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex
import base64
import hashlib

# AES_SECRET_KEY = '1000001010000099'
IV = "2018080720180807"


class AES_ENCRYPT(object):
    def __init__(self, AES_SECRET_KEY):
        self.key = AES_SECRET_KEY
        self.mode = AES.MODE_CBC
        # padding算法
        self.BS = len(self.key)
        self.pad = lambda s: s + (self.BS - len(s) % self.BS) * chr(self.BS - len(s) % self.BS)


    # 加密函数
    def encrypt(self, text):
        cryptor = AES.new(self.key, self.mode, IV)
        self.ciphertext = cryptor.encrypt(self.pad(text))
        # AES加密时候得到的字符串不一定是ascii字符集的，输出到终端或者保存时候可能存在问题，使用base64编码
        return base64.b64encode(self.ciphertext)

    # 解密函数
    def decrypt(self, text):
        decode = base64.b64decode(text)
        cryptor = AES.new(self.key, self.mode, IV)
        plain_text = cryptor.decrypt(decode)
        return plain_text
# aa= AES_ENCRYPT('1000001010000099')
# ss=aa.encrypt("abcd55467")
# print ss
# print aa.decrypt(ss)
